﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using CMN.Common;
using CMN.Entity;
using System.Text.RegularExpressions;
using System.Threading;
using System.Web;
using System.Net;
namespace CMN.Common
{
    public  class CollectHelper
    {


        protected   Regex regValue;
        protected   Regex reg;
        protected int timeOut = 16000;
        public List<Extend_CollectLinkInfoEntity> GetLinks(Extend_CollectSetInfoEntity setLinksInfo)
        {
            string strUrl=string.Empty;
            
            Uri baseUri = new Uri(setLinksInfo.Url);
            string content;
            List<Extend_CollectLinkInfoEntity> collectLinkList=new List<Extend_CollectLinkInfoEntity>();
            Dictionary<string, string> dicUrl = new Dictionary<string, string>();
            try
            {
                for (int i = setLinksInfo.StartNum; i <= setLinksInfo.EndNum; i = i + setLinksInfo.AddNum)
                {
                    if (i == setLinksInfo.StartNum)
                    {
                        strUrl = Utils.ReplaceString(setLinksInfo.Url, "\\{.*\\}", "", false);
                    }
                    else
                    {
                        strUrl = setLinksInfo.Url.Replace("*", i.ToString());
                        strUrl = strUrl.Replace("{", "");
                        strUrl = strUrl.Replace("}", "");
                    }
                    if (!string.IsNullOrEmpty(strUrl.Trim()))
                    {
                        content = Utils.GetRequestString(strUrl, timeOut, 1, Encoding.GetEncoding(setLinksInfo.Encoding), (string.IsNullOrEmpty(setLinksInfo.Cookie) ? null : setLinksInfo.Cookie));
                        if (!string.IsNullOrEmpty(setLinksInfo.ListMatchLeft) && !string.IsNullOrEmpty(setLinksInfo.ListMatchRight))
                        {
                            content = GetCutString(content, setLinksInfo.ListMatchLeft, setLinksInfo.ListMatchRight, true, false);
                        }
                        List<Extend_CollectLinkInfoEntity> collectLinkList2 = new List<Extend_CollectLinkInfoEntity>();
                        collectLinkList2 = GetLinkHref(content, setLinksInfo.IncludeStr, string.Concat(baseUri.Scheme, "://", baseUri.DnsSafeHost));

                        foreach (Extend_CollectLinkInfoEntity linkInfo in collectLinkList2)
                        {
                            if (!dicUrl.ContainsKey(linkInfo.Url))
                            {
                                collectLinkList.Add(linkInfo);
                                dicUrl.Add(linkInfo.Url, linkInfo.Url);
                            }
                        }
                    }

                }
            }
            catch (Exception ex)
            {
                AppLog.Write("采集列表错误"+strUrl, AppLog.LogMessageType.Error, ex);
            }
            return collectLinkList;
        }

        /// <summary>
        /// 内容分页组合
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public Extend_CollectContentEntity GetPagingContent(Extend_CollectSetInfoEntity setLinksInfo, string url)
        {
            Extend_CollectContentEntity collectContent = new Extend_CollectContentEntity();
            List<Extend_CollectLinkInfoEntity> linkList = new List<Extend_CollectLinkInfoEntity>();
            Dictionary<string, string> dicLink = new Dictionary<string, string>();
            StringBuilder reContent = new StringBuilder();
            string reTitle = string.Empty;
            if (string.IsNullOrEmpty(setLinksInfo.PagingReg))
            {
                throw new Exception("内容分页正则表达式为空");
            }
            Uri baseUri = new Uri(url);
            Encoding coding = Encoding.GetEncoding(setLinksInfo.Encoding);
            string content = Utils.GetRequestString(url,timeOut, 1, coding, (string.IsNullOrEmpty(setLinksInfo.Cookie) ? null : setLinksInfo.Cookie));
            linkList= GetLinkHref(content, setLinksInfo.PagingReg, url.Substring(0, (url.LastIndexOf('/') + 1)));
            for (int i = 0; i < linkList.Count; i++)
            {
                if (!dicLink.ContainsKey(linkList[i].Url))
                {

                    dicLink.Add(linkList[i].Url, linkList[i].Url);
                }
            }
            collectContent = GetContent(setLinksInfo,content, false);
            reContent.Append(collectContent.TextContent);
            for (int i = 0; i < linkList.Count; i++)
            {

                if (linkList[i].IsCollect == false && linkList[i].Url != "")
                {
                    string contentTemp = Utils.GetRequestString(linkList[i].Url, timeOut, 1, coding, (string.IsNullOrEmpty(setLinksInfo.Cookie) ? null : setLinksInfo.Cookie));
                    List<Extend_CollectLinkInfoEntity> linkListTemp = new List<Extend_CollectLinkInfoEntity>();
                    linkListTemp = GetLinkHref(contentTemp, setLinksInfo.PagingReg, linkList[i].Url.Substring(0, (linkList[i].Url.LastIndexOf('/') + 1)));
                  foreach (Extend_CollectLinkInfoEntity linkInfo in linkListTemp)
                  {
                      if (!dicLink.ContainsKey( linkInfo.Url))
                      {
                          linkList.Add(linkInfo);
                          dicLink.Add(linkInfo.Url, linkInfo.Url);
                      }
                  }
                  reContent.Append(GetContent(setLinksInfo, contentTemp, true).TextContent);
                    linkList[i].IsCollect = true;
                }
            }

            collectContent.TextContent = reContent.ToString();
            return collectContent;
        }


        /// <summary>
        /// 匹配所有绝对超链接地址
        /// </summary>
        /// <param name="inputstring">获取源码</param>
        /// <param name="str_has">超链接中包括</param>
        /// <param name="oldUrl">获取地址</param>
        /// <returns></returns>
        public List<Extend_CollectLinkInfoEntity> GetLinkHref(string inputstring, string str_has, string baseUrl)
        {
            Common.RegExpression.LinkRegExpression linkRg = new CMN.Common.RegExpression.LinkRegExpression();
            Common.RegExpression.RegContext regContent = new CMN.Common.RegExpression.RegContext(inputstring, 'M', "");
            linkRg.Evaluate(regContent);
            Common.RegExpression.IRegExpression reg;
            Common.RegExpression.RegContext regTemp;
            string a_url = string.Empty;
            List<Extend_CollectLinkInfoEntity> collectLinkList = new List<Extend_CollectLinkInfoEntity>();
            Dictionary<string, string> dicUrlList = new Dictionary<string, string>(10);
            foreach (string strmatch in regContent.Matches)
            {
                reg = new CMN.Common.RegExpression.HrefRegExpression();
                regTemp = new CMN.Common.RegExpression.RegContext(strmatch, 'M', "");
             

                if (reg.IsMatch(strmatch))
                {
                    reg.Evaluate(regTemp);
                    a_url =regTemp.ToString().Replace("href=", "").Replace("HREF=", "").Replace("'", "").Replace("\"", "");
                }
               
                regValue = new Regex("#[^# ]*[ ]*", RegexOptions.IgnoreCase);
                if (regValue.IsMatch(a_url))
                {
                    a_url = a_url.Replace(regValue.Match(a_url).Value, "");
                }
                regValue = new Regex(str_has, RegexOptions.IgnoreCase);
                
                if (regValue.IsMatch(a_url))
                {
                    Extend_CollectLinkInfoEntity lInfo = new Extend_CollectLinkInfoEntity();
                    if (a_url.IndexOf("http://") == -1)
                    {
                        a_url = string.Concat(baseUrl, a_url);
                    }
                    lInfo.Url = a_url;
                    lInfo.CollectNum = 0;
                    lInfo.IsCollect = false;
                    if (!dicUrlList.ContainsKey(a_url))
                    {
                        collectLinkList.Add(lInfo);
                         dicUrlList.Add(a_url, a_url);
                    }
                   
                }
               
            }
            dicUrlList.Clear();
            return collectLinkList;

        }

        /// <summary>
        /// 获取类似第一个meta的标签
        /// </summary>
        /// <param name="inputstring">获取源码</param>
        /// <param name="tagName">获取标签名</param>
        /// <param name="attrname">标签类型如:name</param>
        /// <param name="key">标签类型值如:keywords</param>
        /// <returns>字符串</returns>
        protected string GetSingleTagValue(string inputstring, string tagName, string attrname, string key)
        {
            Regex reg = new Regex("<[ ]*" + tagName + " [^<>]*>", RegexOptions.IgnoreCase);
            MatchCollection matchs = reg.Matches(inputstring);
            string result = string.Empty;
            foreach (Match match in matchs)
            {
                string matchValue = match.Value;
                regValue = new Regex("content=[\"|'][^'\"]*[\"|']", RegexOptions.IgnoreCase);
                if ((matchValue.ToLower().IndexOf(attrname.ToLower() + "=\"" + key.ToLower() + "\"") != -1) || (matchValue.ToLower().IndexOf(attrname.ToLower() + "='" + key.ToLower() + "'") != -1))
                {
                    if (regValue.IsMatch(matchValue))
                    {
                        result = regValue.Match(matchValue).Value;
                        if (!string.IsNullOrEmpty(result))
                        {
                            result = result.Replace("CONTENT=", "").Replace("content=", "").Replace("\"", "").Replace("'", "").Trim();
                        }
                    }
                    return result;
                }
            }
            return string.Empty;
        }

         /// <summary>
        /// 获取类似title标签的值
        /// </summary>
        /// <param name="inputstring">获取源码</param>
        /// <param name="tagName">获取标签名</param>
        /// <returns>返回字符串</returns>
        protected string GetFixTagContent(string inputstring, string tagName)
        {
            reg = new Regex("<[ ]*" + tagName + ".+?" + tagName + "[ ]*>", RegexOptions.IgnoreCase);
            MatchCollection matchs = reg.Matches(inputstring);
            string result = string.Empty;
            string a_content = string.Empty;
            string a_content_left = string.Empty;
            string a_content_right = string.Empty;
            foreach (Match match in matchs)
            {
                a_content = match.Value;

                regValue = new Regex("<[ ]*" + tagName + "[^<>]*>", RegexOptions.IgnoreCase);
                if (regValue.IsMatch(a_content))
                {
                    a_content_left = regValue.Match(a_content).Value;
                }
                regValue = new Regex("</[ ]*" + tagName + "[ ]*>", RegexOptions.IgnoreCase);
                if (regValue.IsMatch(a_content))
                {
                    a_content_right = regValue.Match(a_content).Value;
                }
                if (a_content.Length > 0 && a_content_left != "" && a_content_right != "")
                {
                    result = a_content.Replace(a_content_left, "").Replace(a_content_right, "");
                }
                return result;
            }
            return result;
        }
        /// <summary>
        /// 返回超链接文本
        /// </summary>
        /// <param name="inputstring">含有连接的字符窜</param>
        /// <param name="strInclude">超链接中包含</param>
        /// <returns>string</returns>
        protected List<string> GetLinkText(string inputstring, string strInclude,Int32 maxListLength)
        {
            reg = new Regex("<[ ]*a.+?a[ ]*>", RegexOptions.IgnoreCase);
            MatchCollection matchs = reg.Matches(inputstring);
            string a_content = string.Empty;
          
            List<string> lst = new List<string>();
            foreach (Match match in matchs)
            {
                a_content = match.Value;

                if (a_content.ToLower().IndexOf(strInclude.ToLower()) != -1)
                {
                    // HttpContext.Current.Response.Write(str_has+a_content+"<br/>__________________________");

                    regValue = new Regex("<[ ]*a[^<>]>", RegexOptions.IgnoreCase);
                    if (regValue.IsMatch(a_content))
                    {
                        a_content = a_content.Replace(regValue.Match(a_content).Value, "");
                    }
                    regValue = new Regex("<[ ]*a[ ]*/[ ]*>", RegexOptions.IgnoreCase);
                    if (regValue.IsMatch(a_content))
                    {
                        a_content = a_content.Replace(regValue.Match(a_content).Value, "");
                    }
                    a_content =Utils.RemoveHtml(a_content);
                    if (!lst.Contains(a_content))
                    {
                        if (maxListLength == 0)
                        {
                            lst.Add(a_content);
                        }
                        else
                        {
                            if (lst.Count < maxListLength)
                            {
                                lst.Add(a_content);
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                }
            }

            return lst;
        }

        /// <summary>
        /// 获取指定字符串间的字符串
        /// </summary>
        /// <param name="inputstring">获取源码</param>
        /// <param name="leftTag">开始标记</param>
        /// <param name="rightTag">结束标记</param>
        /// <param name="cutStartNum">开始阶段去除的字符数</param>
        /// <param name="cutStrNum">最后阶段去除的字符数</param>
        /// <param name="isNoHtml">是否包含html标记</param>
        /// <param name="isCut">是否去除开始和结束标记，和最后移除字符数</param>
        /// <returns>返回字符串</returns>
        protected string GetCutString(string inputstring, string leftTag, string rightTag,  bool isNoHtml, bool isCut)
        {
            reg = new Regex(leftTag +@"[\s|\S]*?" + rightTag,RegexOptions.IgnoreCase);
            string str = reg.Match(inputstring).Value;
            
                if (isCut)
                {
                    str = str.Replace(leftTag, "").Replace(rightTag, "");
                   
                }
                if (!isNoHtml)
                {
                    str =Utils.RemoveHtml(str);
                }
            
            return str;

        }

        /// <summary>
        /// 移除符合条件的字符串
        /// </summary>
        /// <param name="inputstring">要检测的字符串</param>
        /// <param name="RemoveRegex">移除的正则表达式</param>
        /// <returns>string</returns>
        public  string RemoveTag(string inputstring, string RemoveRegex)
        {
            reg = new Regex(RemoveRegex, RegexOptions.IgnoreCase);
            MatchCollection matchs = reg.Matches(inputstring);
            foreach (Match match in matchs)
            {
                inputstring = inputstring.Replace(match.Value, string.Empty);
            }
            return inputstring;
        }


        /// <summary>
        /// 移除符合条件的字符串
        /// </summary>
        /// <param name="inputstring">要检测的字符串</param>
        /// <param name="RemoveRegex">移除的正则表达式</param>
        /// <returns>string</returns>
        public string ReplaceTag(string inputstring, string ReplaceRegex, string ReplaceStr)
        {
            reg = new Regex(ReplaceRegex, RegexOptions.IgnoreCase);
            MatchCollection matchs = reg.Matches(inputstring);
            foreach (Match match in matchs)
            {
                inputstring = inputstring.Replace(match.Value, ReplaceStr);
            }
            return inputstring;
        }

        /// <summary>
        /// 获取采集内容
        /// </summary>
        /// <param name="url"></param>
        /// <param name="encoding"></param>
        /// <returns></returns>
        public Extend_CollectContentEntity GetContent(Extend_CollectSetInfoEntity setLinksInfo, string url)
        {
            string inputstring = Utils.GetRequestString(url, timeOut, 0, Encoding.GetEncoding(setLinksInfo.Encoding), (string.IsNullOrEmpty(setLinksInfo.Cookie) ? null : setLinksInfo.Cookie));
          return  GetContent(setLinksInfo,inputstring, false);

        }

        /// <summary>
        /// 获取采集内容
        /// </summary>
        /// <param name="url"></param>
        /// <param name="encoding"></param>
        /// <returns></returns>
        protected  Extend_CollectContentEntity GetContent(Extend_CollectSetInfoEntity setLinksInfo, string content, bool isContent)
        {
            string inputstring = content;
            Extend_CollectContentEntity rCotent = new Extend_CollectContentEntity();
            //分页的话不许采集标题
            if (!isContent)
            {
                rCotent.Title = GetFixTagContent(inputstring, "title");
                if (!string.IsNullOrEmpty(setLinksInfo.TitleReplaceStr))
                    rCotent.Title = ReplaceArray(rCotent.Title, setLinksInfo.TitleReplaceStr);

                //根据标题获取百度关键词
                if (setLinksInfo.IsAutoKeyWord)
                {
                    List<string> lstgoogle = new List<string>();
                    List<string> lstbaidu = new List<string>();
                    try
                    {
                        //string PageUrl = "http://www.google.com.hk/search?hl=zh-CN&newwindow=1&safe=strict&tbo=d&site=&source=hp&q=" + Utils.UrlEncode(rCotent.Title);
                        //string getHtml = Utils.GetRequestString(PageUrl, timeOut, 0, System.Text.Encoding.GetEncoding("gb2312"), (string.IsNullOrEmpty(setLinksInfo.Cookie) ? null : setLinksInfo.Cookie));
                        //lstgoogle = GetLinkText(getHtml, "s?wd", 2);
                        string kewordTemp = Utils.GetRequestString("http://www.baidu.com/s?lm=0&si=&rn=10&ie=utf-8&ct=0&wd=" + Utils.UrlEncode(rCotent.Title) + "&pn=10&ver=0&cl=3&uim=1&usm=0", timeOut, 0, System.Text.Encoding.GetEncoding("utf-8"), (string.IsNullOrEmpty(setLinksInfo.Cookie) ? null : setLinksInfo.Cookie));
                        lstbaidu = GetLinkText(kewordTemp, "s?wd", 2);

                    }
                    catch (Exception ex)
                    {
                        AppLog.Write("采集关键词 " + ex.Message, AppLog.LogMessageType.Error);
                    }
                    List<string> keys = Utils.CombineList(lstgoogle, lstbaidu);
                    if (keys.Count > 0)
                    {
                        rCotent.Tags = Utils.ListToString(keys, ",", 4).Replace("[1]", string.Empty).Replace("[2]", string.Empty).Replace("[3]", string.Empty).Replace("[4]", string.Empty).Replace("[5]", string.Empty).Replace("[6]", string.Empty).Replace("[7]", string.Empty).Replace("[8]", string.Empty).Replace("[9]", string.Empty).Replace("[10]", string.Empty).Replace("上一页", string.Empty).Replace("[11]", string.Empty).Replace("下一页", string.Empty);
                        
                    }

                }
                if (setLinksInfo.IsAutoDescription)
                {
                    rCotent.Summary = GetSingleTagValue(inputstring, "meta", "name", "description");
                    if (string.IsNullOrEmpty(rCotent.Summary))
                    {
                        rCotent.Summary = Utils.GetLengthString(rCotent.TextContent, 200, false);
                    }
                    else
                    {
                        rCotent.Summary = Utils.RemoveHtml(rCotent.Summary);

                    }
                    rCotent.Summary = ReplaceArray(rCotent.Summary, setLinksInfo.ContentReplaseStr);
                }

            }
            rCotent.TextContent = GetCutString(inputstring, setLinksInfo.ContentMatchLeft, setLinksInfo.ContentMatchRight,  setLinksInfo.IsIncludeHtml, true);
            rCotent.TextContent = RemoveTag(rCotent.TextContent, "class[ ]*=['|\"]+[^'\"]+['|\"]+");
            Common.RegExpression.URLRegExpression URLRg = new CMN.Common.RegExpression.URLRegExpression();
            Common.RegExpression.RegContext regContent = new CMN.Common.RegExpression.RegContext(inputstring, 'R', "");
            URLRg.Evaluate(regContent);
            if (setLinksInfo.IsIncludeHtml)
            {
                if (!(setLinksInfo.IsIncludeClass))
                {
                    rCotent.TextContent = RemoveTag(rCotent.TextContent, "class[ ]*=['|\"]+[^'\"]+['|\"]+");

                }
                if (!(setLinksInfo.IsIncludeColor))
                {

                    rCotent.TextContent = RemoveTag(rCotent.TextContent, "color[ ]*=['|\"]+[^'\"]+['|\"]+");
                }
                
                if (!(setLinksInfo.IsIncludeImg))
                {
                    rCotent.TextContent = RemoveTag(rCotent.TextContent, "<[ ]*img[^>]*?>");
                }
                else
                {
                  rCotent.TextContent=  GetImages(rCotent.TextContent, setLinksInfo);
                }

                if (!(setLinksInfo.IsIncludeLink))
                {
                    rCotent.TextContent = RemoveTag(rCotent.TextContent, "<[ ]*a[ ]*[^>]*?>");
                    rCotent.TextContent = RemoveTag(rCotent.TextContent, "<[ ]*/[ ]*a[ ]*>");
                }
                if (!(setLinksInfo.IsIncludeScript))
                {
                    rCotent.TextContent = RemoveTag(rCotent.TextContent, @"<[ ]*script[^>]*?>[\s|\S]*?<[ ]*/[ ]*script[ ]*>");
                }
                if (!(setLinksInfo.IsIncludeStyle))
                {
                    rCotent.TextContent = RemoveTag(rCotent.TextContent, @"<[ ]*style[^>]*?>[\s|\S]*?<[ ]*/[ ]*style[ ]*>");
                    rCotent.TextContent = RemoveTag(rCotent.TextContent, "style[ ]*=['|\"]+[^'\"]+['|\"]+");
                }

            }
            rCotent.TextContent = ReplaceTag(rCotent.TextContent, "<[ ]*div[ ]*[^>]*?>", "<p>");
            rCotent.TextContent = ReplaceTag(rCotent.TextContent, "<[ ]*/[ ]*div[ ]*>", "</p>");
            if (setLinksInfo.ContentReplaseStr != "")
            {
               rCotent.TextContent= ReplaceArray(rCotent.TextContent, setLinksInfo.ContentReplaseStr);
                
            }
            rCotent.TextContent = RemoveTag(rCotent.TextContent, "<[a-zA-Z0-9_:]+>[ ]*<[ ]*/[ ]*[a-zA-Z0-9_:]+>");
            rCotent.TextContent = RemoveTag(rCotent.TextContent, "<[a-zA-Z0-9_:]+>[ ]*<[ ]*/[ ]*[a-zA-Z0-9_:]+>");
            rCotent.TextContent = RemoveTag(rCotent.TextContent, "<[a-zA-Z0-9_:]+>[ ]*<[ ]*/[ ]*[a-zA-Z0-9_:]+>");
            rCotent.TextContent = RemoveTag(rCotent.TextContent, "<[a-zA-Z0-9_:]+>[ ]*<[ ]*/[ ]*[a-zA-Z0-9_:]+>");
            if (setLinksInfo.ContentRemoveRightLength > 0 && rCotent.TextContent.Length > setLinksInfo.ContentRemoveRightLength)
            rCotent.TextContent = rCotent.TextContent.Substring(0, (rCotent.TextContent.Length - setLinksInfo.ContentRemoveRightLength));
            if (setLinksInfo.ContentRemoveLeftLength > 0 && rCotent.TextContent.Length > setLinksInfo.ContentRemoveLeftLength)
            rCotent.TextContent = rCotent.TextContent.Substring(setLinksInfo.ContentRemoveLeftLength, (rCotent.TextContent.Length - setLinksInfo.ContentRemoveLeftLength));
            return rCotent;

        }

        /// <summary>
        /// 批量替换关键词 （abc=aaa;ccc=black)
        /// </summary>
        /// <param name="strOld"></param>
        /// <param name="strReplace"></param>
        /// <returns></returns>
        protected string ReplaceArray(string strOld, string strReplace)
        {
            string[] strList = Utils.SplitString(strReplace, ";");
            for (int i = 0; i < strList.Length; i++)
            {
                string[] strList2 = Utils.SplitString(strList[i], "=");
                if (strList2.Length == 2)
                {
                    if (strList2[1] != "blank")
                    {
                        strOld = Utils.ReplaceString(strOld, strList2[0], strList2[1], false);

                    }
                    else
                        strOld = Utils.ReplaceString(strOld, strList2[0], string.Empty, false);
                }
            }
            return strOld;
        }

        protected string GetImages(string inputstring ,Extend_CollectSetInfoEntity setLinksInfo)
        {
            Common.RegExpression.ImageRegExpression imgRg = new Common.RegExpression.ImageRegExpression();
            Common.RegExpression.RegContext regContent = new CMN.Common.RegExpression.RegContext(inputstring, 'M', "");
            imgRg.Evaluate(regContent);
            Common.RegExpression.IRegExpression reg;
            Common.RegExpression.RegContext regTemp;
            string a_url = string.Empty;
            string TempStr = string.Empty;
            string TitleImg = string.Empty;
            foreach (string strmatch in regContent.Matches)
            {

                    if (TempStr != string.Empty)
                        TempStr += "$Array$" + strmatch.ToString();
                    else
                        TempStr = strmatch.ToString();

            }
            if (TempStr.Length == 0)
            {
                return inputstring;
            }
            string[] TempArr = TempStr.Split(new string[] { "$Array$" }, StringSplitOptions.None);
            TempStr = string.Empty;
            Regex  imgReg = new Regex(@"src\s*=\s*.+?\.(gif|jpg|bmp|jpeg|psd|png)", RegexOptions.IgnoreCase);
            for (int i = 0; i < TempArr.Length; i++)
            {
                MatchCollection  matches = imgReg.Matches(TempArr[i]);
                foreach (Match match in matches)
                {
                    if (TempStr != string.Empty)
                        TempStr += "$Array$" + match.ToString();
                    else
                        TempStr = match.ToString();
                }
            }
            if (TempStr.Length > 0)
            {
                imgReg = new Regex(@"src\s*=\s*", RegexOptions.IgnoreCase);
                TempStr = imgReg.Replace(TempStr, "");
            }
            
            TempStr = TempStr.Replace("\"", "");
            TempStr = TempStr.Replace("'", "");
            TempStr = TempStr.Replace(" ", "");

            //去掉重复图片
            TempArr = TempStr.Split(new string[] { "$Array$" }, StringSplitOptions.None);
            TempStr = string.Empty;
            for (int i = 0; i < TempArr.Length; i++)
            {
                if (TempStr.IndexOf(TempArr[i]) == -1)
                    TempStr += "$Array$" + TempArr[i];
            }
            TempStr = TempStr.Substring(7);

            TempArr = TempStr.Split(new string[] { "$Array$" }, StringSplitOptions.None);
            TempStr = string.Empty;
            string ImageArr = string.Empty;
            for (int i = 0; i < TempArr.Length; i++)
            {
                imgReg = new Regex(TempArr[i]);
                Uri baseUri = new Uri(setLinksInfo.Url);
                string RemoteFileUrl = DefiniteUrl(TempArr[i], string.Concat(baseUri.Scheme, "://", baseUri.DnsSafeHost));
                string LocalPhotoUrl = LocalFileUrl( RemoteFileUrl);
                inputstring = imgReg.Replace(inputstring, LocalPhotoUrl);
               
            }

            return inputstring;

        }

       
        /// <summary>
        /// 远程图片本地化
        /// </summary>
        /// <param name="RemoteFileUrl"></param>
        /// <param name="webUrl">参考网站</param>
        /// <param name="SavePath">虚拟路径，以/结尾</param>
        /// <param name="isSave"></param>
        /// <returns></returns>
        public string LocalFileUrl(string RemoteFileUrl)
        {

            string SavePath = "/Upload/Image/auto/";
            string _LocalFileUrl = RemoteFileUrl;

            string FolderName = DateTime.Now.ToString("yyyy") + "/" + DateTime.Now.ToString("MM") + "/" + DateTime.Now.ToString("dd") + "/";
            Utils.CreateDir(Utils.GetMapPath(SavePath + FolderName));
            string fileType = _LocalFileUrl.Substring(_LocalFileUrl.LastIndexOf('.'));
            string filename = string.Empty;
            System.Guid guid = new Guid();
            guid = Guid.NewGuid();
            filename = guid.ToString();

            filename += fileType;
            if (SaveRemotePhoto(Utils.GetMapPath(SavePath + FolderName  + filename), _LocalFileUrl))
                _LocalFileUrl = SavePath + FolderName  + filename;
            return _LocalFileUrl;
        }
        
        /// <summary>
        /// 取得实际地址
        /// </summary>
        /// <param name="PrimitiveUrl"></param>
        /// <param name="ConsultUrl"></param>
        /// <returns></returns>
        public string DefiniteUrl(string PrimitiveUrl, string ConsultUrl)
        {
            if (!ConsultUrl.StartsWith("http://"))
                ConsultUrl = "http://" + ConsultUrl;
            ConsultUrl = ConsultUrl.Replace("\\", "/");
            ConsultUrl = ConsultUrl.Replace("://", ":\\\\");
            PrimitiveUrl = PrimitiveUrl.Replace("\\", "/");

            if (ConsultUrl.Substring(ConsultUrl.Length - 1) != "/")
            {
                if (ConsultUrl.IndexOf('/') > 0)
                {
                    if (ConsultUrl.Substring(ConsultUrl.LastIndexOf("/"), ConsultUrl.Length - ConsultUrl.LastIndexOf("/")).IndexOf('.') == -1)//不含文件名，有缺陷，暂如此
                        ConsultUrl += "/";
                }
                else//直接是域名
                    ConsultUrl += "/";
            }
            string[] ConArray = ConsultUrl.Split('/');
            string returnStr = string.Empty;
            string[] PriArray;
            int pi = 0;
            if (PrimitiveUrl.Substring(0, 7) == "http://")
                returnStr = PrimitiveUrl.Replace("://", @":\\");
            else if (PrimitiveUrl.Substring(0, 1) == "/")//如果是绝对路径
                returnStr = ConArray[0] + PrimitiveUrl;
            else if (PrimitiveUrl.Substring(0, 2) == "./")//如果是当前路径
            {
                PrimitiveUrl = PrimitiveUrl.Substring(2, PrimitiveUrl.Length - 2);
                if (ConsultUrl.Substring(ConsultUrl.Length - 1) == "/")
                    returnStr = ConsultUrl + PrimitiveUrl;
                else
                    returnStr = ConsultUrl.Substring(0, ConsultUrl.LastIndexOf('/') + 1) + PrimitiveUrl;
            }
            else if (PrimitiveUrl.Substring(0, 3) == "../")//如果是相对父路径
            {
                while (PrimitiveUrl.Substring(0, 3) == "../")
                {
                    PrimitiveUrl = PrimitiveUrl.Substring(3);
                    pi++;
                }
                for (int i = 0; i < ConArray.Length - 1 - pi; i++)
                {
                    if (returnStr.Length > 0)
                        returnStr = returnStr + ConArray[i] + "/";
                    else
                        returnStr = ConArray[i] + "/";
                }
                returnStr = returnStr + PrimitiveUrl;
            }
            else//真实地址
            {
                if (PrimitiveUrl.IndexOf('/') > -1)
                {
                    PriArray = PrimitiveUrl.Split('/');
                    if (PriArray[0].IndexOf('.') > -1)
                    {
                        if (PrimitiveUrl.Substring(PrimitiveUrl.Length - 1) == "/")
                            returnStr = "http://" + PrimitiveUrl;
                        {
                            if (PriArray[PriArray.Length - 1].IndexOf('.') > -1)
                                returnStr = "http:\\" + PrimitiveUrl;
                            else
                                returnStr = "http:\\" + PrimitiveUrl + "/";
                        }
                    }
                    else
                    {
                        if (ConsultUrl.Substring(ConsultUrl.Length - 1) == "/")
                            returnStr = ConsultUrl + PrimitiveUrl;
                        else
                            returnStr = ConsultUrl.Substring(0, ConsultUrl.LastIndexOf('/') + 1) + PrimitiveUrl;
                    }
                }
                else
                {
                    if (PrimitiveUrl.IndexOf('.') > -1)
                    {
                        string lastUrl = ConsultUrl.Substring(ConsultUrl.LastIndexOf('.'));
                        if (ConsultUrl.Substring(ConsultUrl.Length - 1) == "/")
                        {
                            if (lastUrl == "com" || lastUrl == "cn" || lastUrl == "net" || lastUrl == "org")
                                returnStr = "http:\\" + PrimitiveUrl + "/";
                            else
                                returnStr = ConsultUrl + PrimitiveUrl;
                        }
                        else
                        {
                            if (lastUrl == "com" || lastUrl == "cn" || lastUrl == "net" || lastUrl == "org")
                                returnStr = "http:\\" + PrimitiveUrl + "/";
                            else
                                returnStr = ConsultUrl.Substring(0, ConsultUrl.LastIndexOf('/')) + "/" + PrimitiveUrl;
                        }
                    }
                    else
                    {
                        if (ConsultUrl.Substring(ConsultUrl.Length - 1) == "/")
                            returnStr = ConsultUrl + PrimitiveUrl + "/";
                        else
                            returnStr = ConsultUrl.Substring(0, ConsultUrl.LastIndexOf('/')) + "/" + PrimitiveUrl + "/";
                    }
                }
            }

            if (returnStr.Substring(0, 1) == "/")
                returnStr = returnStr.Substring(1);
            if (returnStr.Length > 0)
            {
                returnStr = returnStr.Replace("//", "/");
                returnStr = returnStr.Replace(@":\\", "://");
            }
            else
                returnStr = "$False$";
            return returnStr;
        }
        /// <summary>
        /// 抓取远程图片
        /// </summary>
        /// <param name="fileName">如果是要缩略图，记得图片名以_thumbs.jpg结尾</param>
        /// <param name="RemoteFileUrl"></param>
        /// <param name="width">最后的宽,0表示原来尺寸</param>
        /// <param name="height">最后的高,0表示原来尺寸</param>
        /// <returns></returns>
        public bool SaveRemotePhoto(string fileName, string RemoteFileUrl)
        {
            //try
            try
            {            //为防止图片未能本地化后，内网比较纠结，干脆抓取不到时直接报错
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(RemoteFileUrl);
                request.Timeout = 20000;
                request.KeepAlive = false;
                Stream stream = request.GetResponse().GetResponseStream();
                System.Drawing.Image getImage = System.Drawing.Image.FromStream(stream);

                try
                {
                    getImage.Save(fileName);
                }
                catch
                {
                    System.Drawing.Bitmap t = new System.Drawing.Bitmap(getImage);
                    t.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg);
                    t.Dispose();
                }

                getImage.Dispose();
            }
            catch (Exception ex)
            {
                AppLog.Write("采集图片错误" + RemoteFileUrl, AppLog.LogMessageType.Error, ex);
                return false;
            }
            return true;
        }

    }
}
